package com.hctforgreen.greenservice.ui.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.VideoPartsActivity;
import com.hctforgreen.greenservice.model.VideoTypeEntity.VideoDetailEntity;
import com.hctforgreen.greenservice.utils.AsyncImageLoader;
import com.hctforgreen.greenservice.utils.AsyncImageLoader.ImageCallback;
import com.hctforgreen.greenservice.utils.Md5Util;
import com.hctforgreen.greenservice.utils.ScreenUtil;

/**
 * 视频详情列表适配器
 * 
 * @author dhb 20141015
 * 
 */
public class VideoDetailAdapter extends BaseAdapter implements
		OnItemClickListener {

	private Activity context;
	private List<VideoDetailEntity> myList;
	private GridView gridView;

	private ScreenUtil screenUtil;

	public VideoDetailAdapter(Activity context, List<VideoDetailEntity> myList,
			GridView gridView) {
		this.context = context;
		this.myList = myList;
		this.gridView = gridView;
		gridView.setOnItemClickListener(this);
		screenUtil = new ScreenUtil();
	}

	public int getCount() {
		return myList.size();
	}

	public Object getItem(int position) {
		return myList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_video_detail_layout, null);
			holder.count = (TextView) convertView.findViewById(R.id.tv_count);
			holder.img = (ImageView) convertView.findViewById(R.id.img_video);
			holder.introduce = (TextView) convertView
					.findViewById(R.id.tv_introduce);
			holder.name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.parts = (TextView) convertView.findViewById(R.id.tv_part);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final VideoDetailEntity detailEntity = myList.get(position);

		holder.name.setText(detailEntity.name);
		if (detailEntity.picUrl != null
				&& detailEntity.picUrl.trim().length() > 0) {
			holder.img.setTag(Md5Util.md5(detailEntity.picUrl));
			AsyncImageLoader imageLoader = new AsyncImageLoader();
			Drawable bitmap = imageLoader.loadDrawable(detailEntity.picUrl,
					context, true, new ImageCallback() {

						@Override
						public void imageLoaded(Drawable imageDrawable,
								String imageUrl) {
							ImageView imageView = (ImageView) gridView
									.findViewWithTag(Md5Util
											.md5(detailEntity.picUrl));
							if (imageView != null && imageDrawable != null) {

								BitmapDrawable bitmapDrawable = (BitmapDrawable) imageDrawable;
								Bitmap bitmapTemp = bitmapDrawable.getBitmap();
								if (bitmapTemp != null) {
									int height = 4 * (screenUtil
											.getScreenWidth(context) / 2 - 35) / 5;
									setLayoutHeightAndWidth(0, height,
											imageView);
								}
								imageView.setImageDrawable(imageDrawable);
							}
						}
					});
			if (bitmap != null) {
				BitmapDrawable bitmapDrawable = (BitmapDrawable) bitmap;
				Bitmap bitmapTemp = bitmapDrawable.getBitmap();
				if (bitmapTemp != null) {
					int height = 4 * (screenUtil.getScreenWidth(context) / 2 - 35) / 5;
					setLayoutHeightAndWidth(0, height, holder.img);
				}
				holder.img.setImageDrawable(bitmap);
			}

		} else {
			holder.img.setImageResource(R.drawable.ic_default_pic);
		}

		holder.introduce.setText(detailEntity.introduce);
		holder.count.setText(detailEntity.count);
		holder.parts.setText(context.getResources().getString(
				R.string.video_update_part_head)
				+ detailEntity.parts
				+ context.getResources().getString(
						R.string.video_update_part_footer));
		return convertView;
	}

	class ViewHolder {
		ImageView img;//
		TextView name;// 名称
		TextView introduce;// 简介
		TextView count;// 点击量
		TextView parts;// 更新集数

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		final VideoDetailEntity detailEntity = myList.get(position);
		Intent intent = new Intent(context, VideoPartsActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra("videoId", detailEntity.id);
		intent.putExtra("videoName", detailEntity.name);
		context.startActivity(intent);

	}

	private void setLayoutHeightAndWidth(int weight, int height, View viewGroup) {
		LayoutParams gLayoutParams = viewGroup.getLayoutParams();
		if (weight != 0) {
			gLayoutParams.width = weight;
		}
		if (height != 0) {
			gLayoutParams.height = height;
		}
		viewGroup.setLayoutParams(gLayoutParams);
	}
}